package pacman;

/**
 * The Pacman in the game
 */
public class cPacman {
    private Position pos;

    private int nbLife=5;

    private int Points;
    
    private String Img="pacman";
    
    private iCtrl Controler;
    
    private cCore parent;
    
    private int LastMove=0;
    
    private int CountDead;
    
    private boolean IsDead=false;
    
    private boolean ToggleImage=false;
    
    private Position initPos;

    /**
     * Create a new instance of cPacman
     * @param parent The cCore where is the Pacman
     * @param pos The initial position of the Pacman
     */
    public cPacman(cCore parent, Position pos) {
        this.parent=parent;
        this.pos=pos;
        initPos=pos.clone();
    }

    /**
     * Make the next move of the pacman
     */
    public void Move(){
        if(Controler==null)return;
        
        if(IsDead) return;
        
        Position nextPos=this.pos.clone();
        switch(Controler.getNextMove()){
            case -1:
                nextPos.incX(-1);
                Img="pacman_gauche";
                break;
            case 1:
                nextPos.incX(1);
                Img="pacman_droit";
                break;
            case -2:
                nextPos.incY(1);
                Img="pacman_bas";
                break;
            case 2:
                nextPos.incY(-1);
                Img="pacman_haut";
                break;
            default:
                Img="pacman_bas";
                break;
        }
        
        if(parent.isAccessible(nextPos,this)){
            this.pos=nextPos;
            parent.Eat(nextPos,this);
            parent.Access(nextPos,this);
            LastMove=Controler.getNextMove();
        }else{
            nextPos=this.pos.clone();
            switch(LastMove){
                case -1:
                    nextPos.incX(-1);
                    break;
                case 1:
                    nextPos.incX(1);
                    break;
                case -2:
                    nextPos.incY(1);
                    break;
                case 2:
                    nextPos.incY(-1);
                    break;
            }
            if(parent.isAccessible(nextPos,this)){
                this.pos=nextPos;
                parent.Eat(nextPos,this);
                parent.Access(nextPos,this);
            }else{
                LastMove=0;
            }
        }
        
        if(ToggleImage){
            Img+="_1";
            ToggleImage=false;
        }else{
            Img+="_0";
            ToggleImage=true;
        }
    }
    
    /**
     * Returns the controler of the Pacman
     * @return The iCtrl of the Pacman
     */
    public iCtrl getControler(){
        return Controler;
    }
    
    /**
     * Sets the controler of the Pacman
     * @param ctrl The iCtrl witch control the Pacman
     */
    public void setControler(iCtrl ctrl){
        Controler=ctrl;
    }

    /**
     * Return the current Pacman's position
     * @return The curent Pacman's position
     */
    public Position getPosition() {
        return this.pos;
    }

    /**
     * Set the current Pacman's position
     * @param val The position to set the Pacman
     */
    public void setPosition(Position val) {
        this.pos = val;
    }

    /**
     * Return the number of remaining lifes
     * @return The number of remaining lifes
     */
    public int getNbLife() {
        return nbLife;
    }

    /**
     * Sets the number of remaining lifes
     * @param val The number of remaining lifes to be setted
     */
    public void setNbLife(int val) {
        this.nbLife = val;
    }
    
    /**
     * Kill the pacman
     */
    public void die(){
        if(!IsDead){
            nbLife--;
            if(nbLife==0){
                IsDead=true;
                Img="pacman_dead";
            }else{
                pos=initPos.clone();
            }
        }
    }

    /**
     * Returns the current point number
     * @return The current point number
     */
    public int getPoints() {
        return Points;
    }

    /**
     * Sets the current point number
     * @param val The current point number
     */
    public void setPoints(int val) {
        this.Points = val;
    }
    
    /**
     * Returns the name of the current Pacman's image
     * @return The name of the current Pacman's image
     */
    public String getImageName(){
        return Img;
    }
    
    /**
     * Returns if the Pacman is dead
     * @return True if the Pacman is dead
     */
    public boolean IsDead(){
        return IsDead;
    }
}
